﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;

namespace MetaSharp.Transformation.Tests.Patterns
{
	public class OrTests
	{
		[Fact]
		public async Task ReadsZeroPatternFails()
		{
            var p = Pattern.Or();
            var m = await Pattern.Match(p, "");
            Assert.False(m.Matched);
		}

		[Fact]
		public async Task ReadsOnePatternSuccessfully()
		{
            var p = Pattern.Or(Pattern.Any());
            var m = await Pattern.Match(p, "a");
			Assert.True(m.Matched);
		}

		[Fact]
		public async Task ReadsFirstPatternsSuccessfully()
		{
			var p = Pattern.Or(
				Pattern.Value('a'),
				Pattern.Value('b'));
            var m = await Pattern.Match(p, "a");
            Assert.Equal('a', m.Value);
		}

		[Fact]
		public async Task ReadsSecondPatternSuccessfully()
		{
			var p = Pattern.Or(
				Pattern.Value('a'),
				Pattern.Value('b'));
            var m = await Pattern.Match(p, "b");
			Assert.Equal('b', m.Value);
		}

		[Fact]
		public async Task StopsConsumingInputAfterSuccess()
		{
			var p = Pattern.Or(
                Pattern.Any(), 
                Pattern.Any());
            var m = await Pattern.Match(p, "ab");
            Assert.Equal(1, m.End.Stream.Index);
		}
	}
}
